{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Zipline Coding Exercises\n",
    "\n",
    "In this notebook you will create your own pipeline and create custom factors and filters for it. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Install Packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "!{sys.executable} -m pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from zipline.data import bundles\n",
    "from zipline.data.bundles.csvdir import csvdir_equities\n",
    "\n",
    "\n",
    "# Specify the bundle name\n",
    "bundle_name = 'm4-quiz-eod-quotemedia'\n",
    "\n",
    "# Create an ingest function \n",
    "ingest_func = csvdir_equities(['daily'], bundle_name)\n",
    "\n",
    "# Register the data bundle and its ingest function\n",
    "bundles.register(bundle_name, ingest_func);\n",
    "\n",
    "# Set environment variable 'ZIPLINE_ROOT' to the path where the most recent data is located\n",
    "os.environ['ZIPLINE_ROOT'] = os.path.join(os.getcwd(), '..', '..', 'data', 'module_4_quizzes_eod')\n",
    "\n",
    "# Load the data bundle\n",
    "bundle_data = bundles.load(bundle_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Build an Empty Pipeline with a Screen\n",
    "\n",
    "In the code below create an empty with a screen that filters the pipeline output for stocks with a 60-day average dollar volume greater than \\$50,000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import resources\n",
    "\n",
    "\n",
    "# Create a screen for our Pipeline\n",
    "\n",
    "\n",
    "# Create an empty Pipeline with the given screen\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Create a Pipeline Engine\n",
    "\n",
    "In the code below, create a data loader that loads our data bundle using Zipline's `USEquityPricingLoader`. Then create a function to be passed to the `get_loader` parameter in the pipeline engine. Using the NYSE trading calendar create a pipeline engine using Zipline's `SimplePipelineEngine`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import resources\n",
    "\n",
    "\n",
    "# Set the dataloader\n",
    "\n",
    "\n",
    "# Define the function for the get_loader parameter\n",
    "\n",
    "# Set the trading calendar\n",
    "\n",
    "\n",
    "# Create a Pipeline engine\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Create Factors and Filters For The Pipeline\n",
    "\n",
    "### Factors\n",
    "\n",
    "In the code below, create two factors:\n",
    "\n",
    "1. **Custom Factor**: Create a custom factor, `percent_difference`,  by combining a 30-day average closing price factor and a 60-average closing price factor. The `percent_difference` factor will calculate the difference between the 30-day and 60-average closing price factors, and will then normalize this difference by the 60-average closing price factor.\n",
    "\n",
    "\n",
    "2. **Daily Returns Factor**: Create a factor that returns the daily percent change in closing price. To do this, use Zipline's built-in [DailyReturns](https://www.zipline.io/appendix.html#zipline.pipeline.factors.DailyReturns) factor.\n",
    "\n",
    "Both factors should use the closing prices in the `USEquityPricing`  dataset as inputs. \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "### Filters\n",
    "\n",
    "In the code below, you will create a **Custom Filter**, `tradable_asset`, that returns `True` for the top 20 securities of average dollar volume in a 30 day-window that have a latest closing price above \\$30. To do this, you will create two filters first and them combine them. For the first filter, you can use Zipline's built-in factor `AverageDollarVolume()` and its method `.top()` to select the top 20 assets of average dollar volume in a 30 day-window. To get the latest close price of the `USEquityPricing` dataset you can use `USEquityPricing.close.latest`. Finally you can combine these two filters by using the logical operator `&`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import resources\n",
    "\n",
    "\n",
    "# Create the 30-day average closing price factor\n",
    "\n",
    "\n",
    "# Create the 60-day average closing price factor\n",
    "\n",
    "# Create the custom factor\n",
    "\n",
    "\n",
    "# Create the daily returns factor\n",
    "\n",
    "\n",
    "# Create a filter for the top 20 securities of average dollar volume in a 30 day-window\n",
    "\n",
    "# Create a filter for the latest closing price above $30\n",
    "\n",
    "\n",
    "# Create a custum filter\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Add Factors and Filters To The Pipeline\n",
    "\n",
    "In the code below, add the factors and filters you created above to the pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add the custom factor to the pipeline\n",
    "\n",
    "\n",
    "# Add the daily returns factor to the pipeline\n",
    "\n",
    "\n",
    "# Add the custom filter to the pipeline\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Run The Pipeline\n",
    "\n",
    "In the code below, run the pipeline for the dates given"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set starting and end dates\n",
    "start_date = pd.Timestamp('2014-01-06', tz='utc')\n",
    "end_date = pd.Timestamp('2016-01-05', tz='utc')\n",
    "\n",
    "# Run our pipeline for the given start and end dates\n",
    "\n",
    "\n",
    "# Display the pipeline output\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "[Solution notebook](zipline_coding_exercises_solution.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
